//
//  JSTHistoryViewController.m
//  KYKJPackDemo
//
//  Created by 01 on 17/4/24.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTHistoryViewController.h"
#import "JSTLoadFailView.h"
#import "MJRefresh.h"
#import "JSTAdWebViewController.h"

@interface JSTHistoryViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tabView;

@property (nonatomic, strong) NSMutableArray *data;

@property (nonatomic, strong) JSTNoContentView *noContentView;

@end

@implementation JSTHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"历史记录";
    
    [self createContent];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (void) createContent
{
    JSTCacheModel *model = [[JSTCacheModel alloc] init];
    _data = (NSMutableArray *)[JSTFMDBManager querySqlforTab:model];
    [self.view addSubview:self.tabView];
    if (IsArrEmpty(_data)) {
        self.tabView.tableHeaderView = self.noContentView;
    }
}

- (void) refreshCacheData
{
    JSTCacheModel *model = [[JSTCacheModel alloc] init];
    _data = (NSMutableArray *)[JSTFMDBManager querySqlforTab:model];
    if (IsArrEmpty(_data)) {
        self.tabView.tableHeaderView = self.noContentView;
    }else
    {
        self.tabView.tableHeaderView = [[UIView alloc] init];
        [self.tabView reloadData];
    }
    [self.tabView headerEndRefreshing];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"historyCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    JSTCacheModel *model = _data[indexPath.row];
    cell.textLabel.text = model.title;
    cell.detailTextLabel.text = model.url;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DLog(@"------- data.count =%ld", (unsigned long)_data.count);
    return _data.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JSTAdWebViewController *webView = [[JSTAdWebViewController alloc] init];
    JSTCacheModel *model = _data[indexPath.row];
    webView.urlStr = model.url;
    [self.navigationController pushViewController:webView animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   UITableViewCellEditingStyleDelete;
}

/*改变删除按钮的title*/
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

/*删除用到的函数*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        JSTCacheModel *model = [[JSTCacheModel alloc] init];
        model = _data[indexPath.row];
        [JSTFMDBManager deleteModelInDatabase:model];
        
        //删除数组里的数据
        [_data removeObjectAtIndex:indexPath.row];
        //删除对应数据的cell
        [tableView   deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (UITableView *)tabView
{
    if (!_tabView)
    {
        _tabView = [[UITableView alloc] init];
        _tabView.frame = self.view.bounds;
        _tabView.delegate = self;
        _tabView.dataSource= self;
        _tabView.tableFooterView = [[UIView alloc] init];
        [_tabView addHeaderWithTarget:self action:@selector(refreshCacheData)];
//        [_tabView addHeaderWithTarget:self action:@selector(refreshCacheData)];
//        [_tabView setHeaderPullToRefreshText:@"下拉获取最浏览记录"];
//        [_tabView setHeaderReleaseToRefreshText:@"正在获取最新浏览记录"];
    }
    return _tabView;
}

- (JSTNoContentView *)noContentView
{
    if (!_noContentView) {
        // 暂无内容页面高度为屏幕高度 - 导航栏高度（含状态栏高度 64px）- tabBar高度（49px）
        _noContentView = [[JSTNoContentView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight - 64 - 49)];
    }
    return _noContentView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
